<?php

namespace app\api\controller;

class Stat extends BaseApi
{

    //数据展示
    public function datashow()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $start_date =  isset($this->params['start_date']) ? $this->params['start_date'] :  date('Y-m-d 00:00', strtotime("-6 day"));

        $end_date = isset($this->params['end_date']) ? $this->params['end_date'] : date('Y-m-d 23:59', time());

        $data = array();
        $time_arr = [];
        $day = daysBetweenDates($start_date,$end_date);
        for ($i = 0;$i <= $day;$i++){
            $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
            $end_time = $time+86400;
            $stat_balance_money = model('member_account')->getSum([
                ['member_id','=',$this->member_id],
                ['from_type','=','order'],
                ['account_type','=','balance_money'],
                ['create_time', 'between', [$time, $end_time]],
            ],'account_data');
            $data['balance_money'][$i] = abs($stat_balance_money);
            $time_arr[$i] = date('Y-m-d',$time);
        }
        $echart_data = [
            'time'=>$time_arr,
            'data'=>$data
        ];

        $from_type = [
            'recommend_card_one_commission',
            'recommend_card_two_commission',
            //'hotel_order_one_commission',
            //'hotel_order_two_commission'
        ];

        //余额
        $member_account_model = new \app\model\member\MemberAccount();
        $total_recommend_money = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','in',$from_type],
            ['account_type','=','balance_money']
        ],'account_data');

        $fenxiao_info = model('fenxiao')->getInfo(['member_id'=>$this->member_id],'fenxiao_id,member_id,one_child_fenxiao_num,two_child_fenxiao_num');

        //酒店订单
        $hotel_fenxiao_order_count = model('hotel_fenxiao_order')->getCount([
            ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['one_commission|two_commission','>',0]
        ]);
        //本地生活+商城
        $fenxiao_order_count = model('fenxiao_order')->getCount([
            ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['one_commission|two_commission','>',0]
        ]);

        $startOfDay = date('Y-m-d 00:00:00');

        $endOfDay = date('Y-m-d 23:59:59');
        //今日推荐会员收益
        $member_account_model = new \app\model\member\MemberAccount();
        $profit_from_type = [
            'recommend_card_one_commission',
            'recommend_card_two_commission',
            'hotel_order_one_commission',
            'hotel_order_two_commission',
            'fenxiao',
            'mrna_commission'
        ];
        $today_profit_money = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','in',$profit_from_type],
            ['account_type','=','balance_money'],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]],
        ],'account_data');
        //今日推荐的会员数
        $one_card_order_count = model('member_level_order')->getCount([
            ['one_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['pay_status','=',1],
            ['pay_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);

        $two_card_order_count = model('member_level_order')->getCount([
            ['two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['pay_status','=',1],
            ['pay_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);

        //当天酒店分销订单
        $today_hotel_fenxiao_order_count = model('hotel_fenxiao_order')->getCount([
            ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['one_commission|two_commission','>',0],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);
        //当天商城+本地生活分销订单
        $fenxiao_order_count = model('fenxiao_order')->getCount([
            ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
            ['one_commission|two_commission','>',0],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);
        $balance_money = [
            'total_recommend_money'=> $total_recommend_money['data'],//推荐会员奖励的总额
            'total_recommend_count'=>$fenxiao_info['one_child_fenxiao_num']+$fenxiao_info['two_child_fenxiao_num'],//推荐的会员数
            'total_one_recommend_count'=>$fenxiao_info['one_child_fenxiao_num'],//一级
            'total_two_recommend_count'=>$fenxiao_info['two_child_fenxiao_num'],//二级
            'total_fenxiao_order_count'=>$hotel_fenxiao_order_count+$fenxiao_order_count,//订单数
            'today_profit_money'=>$today_profit_money['data'],//今日收益
            'today_recommend_count'=>$one_card_order_count+$two_card_order_count,//今日会员数
            'today_one_recommend_count'=>$one_card_order_count,//今日一级
            'today_two_recommend_count'=>$two_card_order_count,//今日二级
            'today_hotel_fenxiao_order_count'=>$today_hotel_fenxiao_order_count+$fenxiao_order_count//今日订单数
        ];
        //余额走势图



        //积分统计
        //累计得总积分
        $total_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            //['from_type','in',$from_type],
            ['account_type','=','point'],
            ['account_data','>',0]
        ],'account_data');

        //累计消费
        $total_consume_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            //['from_type','in',$from_type],
            ['from_type','=','pointcash'],
            ['account_type','=','point'],
        ],'account_data');

        //今日消费
        $today_consume_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            //['from_type','in',$from_type],
            ['from_type','=','pointcash'],
            ['account_type','=','point'],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ],'account_data');

        $week_start_date = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
        $week_end_date = date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y')));

        //本周消费
        $week_consume_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','=','pointcash'],
            ['account_type','=','point'],
            ['create_time', 'between', [date_to_time($week_start_date), date_to_time($week_end_date)]]
        ],'account_data');

        $point = [
            'total_point'=>abs($total_point['data']),//总积分(累计得到)
            'today_consume_point'=>abs($today_consume_point['data']),//当天消费
            'week_consume_point'=>abs($week_consume_point['data']),//本周消费
            'total_consume_point'=>abs($total_consume_point['data']),//累计消费
        ];

        $data = [
            'balance_money'=>$balance_money,
            'point'=>$point,
            'echart_data'=>$echart_data
        ];
        return $this->response($this->success($data));
    }

    //消费相关
    public function consumeAbout()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $start_date =  isset($this->params['start_date']) ? $this->params['start_date'] :  date('Y-m-d 00:00', strtotime("-6 day"));

        $end_date = isset($this->params['end_date']) ? $this->params['end_date'] : date('Y-m-d 23:59', time());

        $startOfDay = date('Y-m-d 00:00:00');

        $endOfDay = date('Y-m-d 23:59:59');

        $data = array();
        $time_arr = [];
        $day = daysBetweenDates($start_date,$end_date);
        for ($i = 0;$i <= $day;$i++){
            $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
            $end_time = $time+86400;
            $balance_money = model('member_account')->getSum([
                ['member_id','=',$this->member_id],
                ['from_type','=','order'],
                ['account_type','=','balance_money'],
                ['create_time', 'between', [$time, $end_time]],
            ],'account_data');
            $data['balance_money'][$i] = abs($balance_money);

            $memberrecharge = model('member_account')->getSum([
                ['member_id','=',$this->member_id],
                ['from_type','=','memberrecharge'],
                ['account_type','=','balance_money'],
                ['create_time', 'between', [$time, $end_time]],
            ],'account_data');
            $data['memberrecharge'][$i] = abs($memberrecharge);

            $point = model('member_account')->getSum([
                ['member_id','=',$this->member_id],
                ['from_type','=','pointcash'],
                ['account_type','=','point'],
                ['create_time', 'between', [$time, $end_time]],
            ],'account_data');
            $data['point'][$i] = abs($point);

            $time_arr[$i] = date('Y-m-d',$time);
        }

        $balance_echart_data = [
            'time'=>$time_arr,
            'data'=>$data
        ];

        $member_account_model = new \app\model\member\MemberAccount();
        $total_income_money = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','=','order'],
            ['account_type','=','balance_money'],
        ],'account_data');

        $today_income_money = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','=','order'],
            ['account_type','=','balance_money'],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ],'account_data');

        $total_hotel_order_count = model('hotel_order')->getCount([
            ['member_id','=',$this->member_id],
            ['pay_status','=',1],
            ['balance_money','>',0]
        ]);

        $today_hotel_order_count = model('hotel_order')->getCount([
            ['member_id','=',$this->member_id],
            ['pay_status','=',1],
            ['balance_money','>',0],
            ['pay_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);

        $balance_data = [
            'total_income_money'=>abs($total_income_money['data']),//累计消费
            'today_income_money'=>abs($today_income_money['data']),//今日消费
            'total_hotel_order_count'=>$total_hotel_order_count,//用余额支付的订单总数
            'today_hotel_order_count'=>$today_hotel_order_count,//用余额支付的今日订单
        ];

        $total_income_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','=','pointcash'],
            ['account_type','=','point'],
        ],'account_data');

        $today_income_point = $member_account_model->getMemberAccountSum([
            ['member_id','=',$this->member_id],
            ['from_type','=','pointcash'],
            ['account_type','=','point'],
            ['create_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ],'account_data');

        $total_hotel_point_order_count = model('hotel_order')->getCount([
            ['member_id','=',$this->member_id],
            ['pay_status','=',1],
            ['point_money','>',0]
        ]);

        $today_hotel_point_order_count = model('hotel_order')->getCount([
            ['member_id','=',$this->member_id],
            ['pay_status','=',1],
            ['point_money','>',0],
            ['pay_time', 'between', [date_to_time($startOfDay), date_to_time($endOfDay)]]
        ]);
        $point_data = [
            'total_income_money'=>abs($total_income_point['data']),//累计消费
            'today_income_money'=>abs($today_income_point['data']),//今日消费
            'total_hotel_order_count'=>$total_hotel_point_order_count,//用积分抵扣的订单总数
            'today_hotel_order_count'=>$today_hotel_point_order_count,//用积分抵扣的今日订单
        ];


       /* $point_echart_data = [

        ];*/
        $data = [
            'balance_data'=>$balance_data,
            'echart_data'=>$balance_echart_data,
            'point_data'=>$point_data,
            //'point_echart_data'=>$point_echart_data,
        ];

        return $this->response($this->success($data));
    }


    //收入相关
    public function incomeAbout()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $start_date =  isset($this->params['start_date']) ? $this->params['start_date'] :  date('Y-m-d 00:00', strtotime("-6 day"));

        $end_date = isset($this->params['end_date']) ? $this->params['end_date'] : date('Y-m-d 23:59', time());

        $startOfDay = date('Y-m-d 00:00:00');

        $endOfDay = date('Y-m-d 23:59:59');

        $fenxiao_info = model('fenxiao')->getInfo(['member_id'=>$this->member_id],'fenxiao_id,member_id,one_child_fenxiao_num,two_child_fenxiao_num');


        $data = array();
        $time_arr = [];
        $day = daysBetweenDates($start_date,$end_date);
        for ($i = 0;$i <= $day;$i++){
            $time = strtotime(date('Y-m-d',strtotime("-".($day-$i)." day")));
            $end_time = $time+86400;

            $store_card_count = model('member_level_order')->getCount([
                ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
                ['member_card_type','=','1'],
                ['pay_status','=','1'],
                ['pay_time', 'between', [$time, $end_time]],
            ]);
            $data['store_card_count'][$i] = $store_card_count;

            $person_card_count = model('member_level_order')->getCount([
                ['one_fenxiao_id|two_fenxiao_id','=',$fenxiao_info['fenxiao_id']],
                ['member_card_type','=','2'],
                ['pay_status','=','1'],
                ['pay_time', 'between', [$time, $end_time]],
            ]);
            $data['person_card_count'][$i] = $person_card_count;

            $mrna_balance = model('member_account')->getSum([
                ['member_id', '=', $this->member_id],
                ['account_type', '=', 'balance_money'],
                ['account_data', '>', 0],
                ['from_type', '=', 'mrna_commission'],
                ['create_time', 'between', [$time, $end_time]],
            ],'account_data');

            $data['mrna_balance'][$i] = $mrna_balance;

            $time_arr[$i] = date('Y-m-d',$time);
        }

        $echart_data = [
            'time'=>$time_arr,
            'data'=>$data
        ];

        /*$mrna_echart_data = [
            'time'=>$mrna_time_arr,
            'data'=>$mrna_data
        ];*/

        $return_data = [
            'echart_data'=>$echart_data,//统计图
            'recommend_count'=>model('fenxiao')->getCount(['parent'=>$fenxiao_info['fenxiao_id']]),//直推总人数
            //'mrna_echart_data'=>$mrna_echart_data,//权益卡 mrna
        ];
        return $this->response($this->success($return_data));
    }
}